/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddnsserver;

import java.util.*;
import java.io.*;
/**
 *
 * @author Do Viet Trung
 */
public class Host {
    public enum TYPE_HOST {
        SOA,
        NS,
        MX,
        A,
        CNAME,
        UNKNOWN
    }

    private TYPE_HOST Type;
    // hostA	Host (A)	192.168.1.3
    // HostName: hostA
    // Address: 192.168.1.3
    // Description: Host
    // Type: A
    // Other: Số trong hai dấu [], ví dụ trong Mail Exchanger (MX) [10]  , Other = 10;
    private String HostName,
            Address,
            Description,
            Other; 

    public Host() {
        Type = TYPE_HOST.UNKNOWN;
        HostName = "";
        Address = "";
        Description = "";
        Other = "";
    }

    public Host(TYPE_HOST type,
                String hostName, String address, String description, String other) {
        Type = type;
        HostName = hostName;
        Address = address;
        Description = description;
        Other = other;
    }

    public Host(Host obj) {
        Type = obj.Type;
        HostName = obj.HostName;
        Address = obj.Address;
        Description = obj.Description;
        Other = obj.Other;
    }

    public Host copyHost(Host obj) {
        Type = obj.Type;
        HostName = obj.HostName;
        Address = obj.Address;
        Description = obj.Description;
        Other = obj.Other;
        return this;
    }

    public Vector<Host> buildHost(String path) throws IOException {
        Vector<Host> host = new Vector<Host>();
        
        if(!path.isEmpty()) {
             BufferedReader br = new BufferedReader(new FileReader(path));
                String txt;
                int type = -1;
                String type_host = "";
                String other = "";
                String host_name = "";
                String address = "";
                String description = "";
                while(true)
                {
                    type = 0;
                    txt = br.readLine();

                    if(txt == null)
                    {
                        break;
                    }
                    if(txt.startsWith("Name	Type	Data"))
                    {
                        continue;
                    }
                    if(txt.startsWith("(same as parent folder)"))
                    {
                        int idx = txt.indexOf(')');
                        
                        txt = JProcess.subString(txt, idx + 1, txt.length() - 1);
                        type = 1;
                    }
                        if(txt != null)
                        {
                            Vector<String> lst = JProcess.parseString(txt, '\t');
                            if(lst.size() < 2)continue;
                            Host h = new Host();
                            
                            if(type == 1)//special record
                            {
                                type_host = JProcess.getStringBetween(lst.elementAt(0), '(', ')');
                                other  = JProcess.getStringBetween(lst.elementAt(1), '[', ']');
                                int idx = lst.elementAt(0).indexOf('(');
                                if(idx >= 0)
                                {
                                    description  = JProcess.subString(lst.elementAt(0), 0, idx);
                                    description = description.trim();
                                }
                                idx = lst.elementAt(1).indexOf(',');
                                if(idx >=0)
                                {
                                    host_name = JProcess.subString(lst.elementAt(1), idx +1, lst.elementAt(1).length());

                                    host_name = host_name.trim();
                                }
                                else
                                {
                                    idx = lst.elementAt(1).indexOf(']');
                                    if(idx >=0)
                                    {
                                        host_name = JProcess.subString(lst.elementAt(1), idx + 1, lst.elementAt(1).length());
                                    }
                                    else
                                    {
                                         host_name = lst.elementAt(1);
                                    }
                                    host_name = host_name.trim();
                                }
                                idx = host_name.indexOf('.');
                                if(!host_name.endsWith("."))
                                {
                                    host_name = host_name + '.';
                                }
                                if(idx > 0)
                                {
                                    address = JProcess.subString(host_name, 0, idx);
                                }
                            }
                            else if (type == 0 )//host,cname
                            {
                                if(lst.size() < 3)continue;
                                type_host = JProcess.getStringBetween(lst.elementAt(1), '(', ')');
                                other = "";
                                int idx = lst.elementAt(1).indexOf('(');
                                if(idx >= 0)
                                {
                                    description  = JProcess.subString(lst.elementAt(1), 0, idx);
                                    description = description.trim();
                                }
                                host_name = lst.elementAt(0);
                                address = lst.elementAt(2);
                                if(address.endsWith("."))
                                {
                                    idx = address.indexOf('.');
                                    if(idx >= 0)
                                    {
                                        address = JProcess.subString(address, 0, idx);
                                    }
                                }
                            }
                            h.Address = address;
                            h.HostName = host_name;
                            h.Description = description;
                            h.Other = other;
                            h.Type = switchTypeHost(type_host);
                            host.add(h);
                        }
                }

            }
        return host;
        }

    //Property
    public String getHostName()
    {
        return HostName;
    }
    public String getAddress()
    {
        return Address;
    }
    public String getDescription()
    {
        return Description;
    }
    public String getOther()
    {
        return Other;
    }
    public TYPE_HOST getHostType()
    {
        return Type;
    }

    public void setHostName(String hostName)
    {
        if(hostName != null)
        {
            HostName = hostName;
        }
    }
     public void setAddress(String address)
    {
        if(address != null)
        {
            Address = address;
        }
    }
      public void setDescription(String description)
    {
        if(description != null)
        {
            Description = description;
        }
    }
       public void setOther(String other)
    {
           if(other == null)
           {
               Other = other;
           }
    }
        public void setHostType(TYPE_HOST type)
    {
        Type = type;
    }
    //end property
    private TYPE_HOST switchTypeHost(String type_host)
    {
        if(type_host.startsWith("SOA"))
        {
            return TYPE_HOST.SOA;
        }
        if(type_host.startsWith("NS"))
        {
            return TYPE_HOST.NS;
        }
        if(type_host.startsWith("MX"))
        {
            return TYPE_HOST.MX;
        }
        if(type_host.startsWith("A"))
        {
            return TYPE_HOST.A;
        }
        if(type_host.startsWith("CNAME"))
        {
            return TYPE_HOST.CNAME;
        }
        return TYPE_HOST.UNKNOWN;
    }

}
